/*This file is part of Legend of the Dark Blade.

    Legend of the Dark Blade is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Legend of the Dark Blade is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser Public License for more details.

    You should have received a copy of the GNU Lesser Public License
    along with Legend of the Dark Blade.  If not, see <http://www.gnu.org/licenses/>.*/

#include "global.h"
//#include "ball.h"
//#include "main.cpp"

Player::Player(){
      health=10;
      respawn();
}

Player::~Player(){

}

void Player::respawn(){
      x=spawn_x;
      y=spawn_y;
      xx=0;
      yy=0;
      dir=1;

      onground = false;
      canjump = false;

      if(health<=0)SDL_Quit();

}

void Player::damage(int x){
    health-=x;
}

void Player::move(bool left, bool right, bool jump){
     if(left)xx-=VELMOVINGADD;
     if(right)xx+=VELMOVINGADD;
     if(onground&&canjump&&jump){
                        yy-=VELJUMP;
                        onground = false;
                        canjump = false;
                        }
     if(!jump){
               canjump=true;
               if(!onground&&yy<0)yy=yy/1.25;
               }
     yy+=GRAVITATION;

     if(onground){
                  if(xx>0){
                           if(xx-VELMOVINGFRICTION<0)xx=0;
                           else xx-=VELMOVINGFRICTION;
                           }
                  if(xx<0){
                           if(xx+VELMOVINGFRICTION>0)xx=0;
                           else xx+=VELMOVINGFRICTION;
                           }
                  }



     if(xx>=VELMOVING)xx=VELMOVING;
     if(xx<=0-VELMOVING)xx=0-VELMOVING;

     if(yy>=VELTERMVEL)yy=VELTERMVEL;
     if(yy<=0-VELTERMVEL)yy=0-VELTERMVEL;

     old_x = x;
     old_y = y;

     x+=xx;
     y+=yy;
     if(xx>0)dir=1;
     if(xx<0)dir=-1;

     if(yy>GRAVITATION+1)onground=false;

     /*if(y>SCREENH-PH){
          y=SCREENH-PH;
          yy=0;
          onground=true;
     }
     if(y<0){
          y=0;
          yy=0;
     }
     if(x>SCREENW-PW){
          x=SCREENW-PW;
          xx=0;
     }
     if(x<0){
          x=0;
          xx=0;
     }*/
     if(y>SCREENH+PH){
        damage(1);
        respawn();
     }
}

void Player::collide(int map[15][2000]){//map
    int x_trim = x+PLTRIM;
    int y_trim = y+PTTRIM;
    int old_x_trim = old_x+PLTRIM;
    int old_y_trim = old_y+PTTRIM;
    for(int map_x = 0; map_x<MAPWIDTH; map_x++){
                 for(int map_y = 0; map_y<MAPHEIGHT; map_y++){

                     int tile = map[map_y][map_x];

                     int tile_x = map_x*TILEWIDTH;
                     int tile_y = map_y*TILEHEIGHT;

                     bool collidex = boxcollide(x_trim,old_y_trim,PWTRIM,PHTRIM,tile_x,tile_y,TILEWIDTH,TILEHEIGHT);
                     bool collidey = boxcollide(old_x_trim,y_trim,PWTRIM,PHTRIM,tile_x,tile_y,TILEWIDTH,TILEHEIGHT);

                     bool collide = collidex||collidey;
                     if(tile>0&&tile<20){
                         if(collidex){
                              //x=old_x;
                              if(xx>0){
                                   x=tile_x-PW+PRTRIM;
                              }
                              if(xx<0){
                                   x=tile_x+TILEWIDTH-PLTRIM;
                              }
                              xx=0;
                         }
                         if(collidey){
                              //y=old_y;
                              if(yy>0){
                                   y=tile_y-PH+PBTRIM;
                                   onground=true;
                              }
                              if(yy<0){
                                   y=tile_y+TILEHEIGHT-PTTRIM;
                              }
                              yy=0;
                         }
                     }
                     /*if(tile==2&&collide){
                                          yy=-VELSUPERJUMP;
                                          onground=false;
                                          }
                     if(tile==3&&collide){
                                          respawn();
                                          }*/

                 }
     }
}

void Player::draw(){
     int z=1;
     z=(int)(x/6);
     if(x>7) z=(int)(x/6)%7;

     if(onground){
         if(xx>0){
            //z/=4;
            apply_surface((int)x-cam_x,(int)y-cam_y,player,screen,&pclip[z+2]);
        }
        if(xx<0){
            apply_surface((int)x-cam_x,(int)y-cam_y,player,screen,&pclip[z+2+9]);
        }
        if(xx==0){
            if(dir==1){
                apply_surface((int)x-cam_x,(int)y-cam_y,player,screen,&pclip[1]);
            }
            if(dir==-1){
                apply_surface((int)x-cam_x,(int)y-cam_y,player,screen,&pclip[10]);
            }
        }
     }
     else{
            if(dir==1){
                apply_surface((int)x-cam_x,(int)y-cam_y,player,screen,&pclip[8]);
            }
            if(dir==-1){
                apply_surface((int)x-cam_x,(int)y-cam_y,player,screen,&pclip[17]);
            }
     }
}

